# Contributor: Natanael Copa <ncopa@alpinelinux.org>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=lua-stringy
pkgver=0.5.1
_pkgver=${pkgver%.*}-${pkgver##*.}
pkgrel=3
pkgdesc="Lua string utility library"
url="https://github.com/brentp/lua-stringy/"
arch="all"
license="MIT"
source="$pkgname-$pkgver.tar.gz::https://github.com/brentp/lua-stringy/archive/v$_pkgver.tar.gz
	makefile.patch"
builddir="$srcdir/$pkgname-$_pkgver/stringy"

_luaversions="5.1 5.2 5.3 5.4"
for _v in $_luaversions; do
	checkdepends="$checkdepends lua$_v"
	makedepends="$makedepends lua$_v-dev"
	subpackages="$subpackages lua$_v-${pkgname#lua-}:_subpackage"
done

prepare() {
	default_prepare

	local lver; for lver in $_luaversions; do
		cp -r "$builddir" "$builddir-$lver"
	done
}

build() {
	local lver; for lver in $_luaversions; do
		msg "Building for Lua $lver..."
		CFLAGS="$CFLAGS -std=gnu89" \
		make -C "$builddir-$lver" \
			CC="${CC:-gcc}" \
			LUA_PC="lua$lver" \
			LUA="lua$lver"
	done
}

check() {
	local lver; for lver in $_luaversions; do
		msg "Testing on lua$lver..."
		cd "$builddir-${lver/jit/5.1}"
		lua$lver stringy_test.lua
	done
}

package() {
	mkdir -p "$pkgdir"
}

_subpackage() {
	local lver="${subpkgname:3:3}"
	pkgdesc="$pkgdesc (for Lua $lver)"
	depends="lua$lver"
	install_if="$pkgname=$pkgver-r$pkgrel lua$lver"

	local cmoddir="$subpkgdir/$(pkg-config --variable=INSTALL_CMOD lua$lver)"
	local rockdir="$subpkgdir/usr/lib/luarocks/rocks-$lver/$pkgname/$_pkgver"

	install -m 755 -D "$builddir-$lver"/stringy.so "$cmoddir"/stringy.so

	mkdir -p "$rockdir"
	echo 'rock_manifest = {}' > "$rockdir"/rock_manifest
}

sha512sums="119c3ea34d380e2e32d9ebd8ef1bec01cd980455852cb4e1053a929afc69d43149cb7bea70c264b97be239e39d3bd376de21b4f349730ed5e78a47a1ba34b44a  lua-stringy-0.5.1.tar.gz
c3a8a6705e1b39a99e117f57e49263f676905775452787a1c8dc5a30eda51bc652d9bfe6c975bcb4ded9cb60a6bf70f6654dd31575f22f932be58474de01efba  makefile.patch"
